package com.lc.question.controller;

import com.lc.question.pojo.PageBean;
import com.lc.question.pojo.StuTab;
import com.lc.question.pojo.Student;
import com.lc.question.pojo.UserTab;
import com.lc.question.service.impl.StudentServiceImpl;
import com.lc.question.service.impl.UserTabServiceImpl;
import com.lc.question.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@Api(tags = "TeacherController|学生接口类")
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentServiceImpl studentService;
    @Autowired
    UserTabServiceImpl userTabService;

    @ApiOperation(value = "通过学号ID查找学生信息")
    @PostMapping("/findById")
    @ResponseBody
    public Student findStudentById(String stu_Id) {
        Student student = studentService.findStudentById(stu_Id);
        return student;
    }

    @ApiOperation(value = "通过条件信息查找学生集合list")
    @PostMapping("/findById2")
    @ResponseBody
    public List<Student> findStudentById2(@RequestBody Student student) {
        List<Student> students = studentService.studentListById2(student);
        return students;
    }

    @ApiOperation(value = "list(所有学生集合)")
    @PostMapping("/list")
    @ResponseBody
    public List<Student> studentList(){
        List<Student> students = studentService.studentList();
        return students;
    }

    @ApiOperation(value = "add(新增学生)")
    @PostMapping("/add")
    @ResponseBody
    public void addStudent(@RequestBody Student student){
        studentService.addStudent(student);
    }

    @ApiOperation(value = "list_tab(学生信息展示表)")
    @PostMapping("/list_tab")
    @ResponseBody
    public PageBean<StuTab> studentList(@RequestBody StuTab oldStuTab, @RequestParam("currentPage") int currentPage, @RequestParam("total") int total){
        List<StuTab> stuTabs = new ArrayList<>();

        UserTab userTab = oldStuTab.getUserTab();
        Student stu = oldStuTab.getStudent();//给定要查询的student模板
        //传入tab_Id和par_state
        long totalNumber = userTabService.tabTotal(userTab);//总条数
        PageBean<StuTab> planPageBean = new PageBean<StuTab>(currentPage,total,totalNumber);
        int startIndex = planPageBean.getStartIndex();
        List<UserTab> tabs = userTabService.findTabChange_SurPar(userTab,startIndex,total);
        for (UserTab tab : tabs) {
            String user_id = tab.getUser_Id();
            stu.setStu_Id(user_id);
            //添加
            Student student = studentService.studentById(stu);
            //添加stuTab
            if (student!=null){
                StuTab stuTab = new StuTab(student,tab);
                stuTabs.add(stuTab);
            }
        }
        planPageBean.setPageDate(stuTabs);//设置返回数据集
        return planPageBean;
    }


}
